package net.zio.kamenriderzioaddon.ridersystem.behavior.impl;

import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.ChatFormatting;
import net.zio.kamenriderzioaddon.enums.HenshinState;
import net.zio.kamenriderzioaddon.enums.SoundType;
import net.zio.kamenriderzioaddon.ridersystem.behavior.IHenshinBaseBehavior;
import net.zio.kamenriderzioaddon.ridersystem.data.PlayerDataManager;
import net.zio.kamenriderzioaddon.ridersystem.registry.BaseRiderConfig;
import net.zio.kamenriderzioaddon.ridersystem.util.HenshinUtils;

public class DefaultHenshinBaseBehavior implements IHenshinBaseBehavior {

    @Override
    public void handleHenshin(Player player, BaseRiderConfig config) {
        HenshinState currentState = config.getCurrentState(player);

        switch (currentState) {
            case INACTIVE:
                enterStandbyState(player, config);
                break;

            case STANDBY:
                completeTransformation(player, config);
                break;

            case ACTIVE:
                break;
        }
    }

    @Override
    public void handleQuickTransformation(Player player, BaseRiderConfig config) {
        // 清除现有状态
        PlayerDataManager.get(player).clearGearData();
        config.setCurrentState(player, HenshinState.INACTIVE);

        // 保存玩家当前装备（全部）
        CompoundTag gearData = HenshinUtils.savePlayerGear(player);
        PlayerDataManager.get(player).setActiveGear(gearData);

        // 特别保存腿甲和主手物品到待机装备
        CompoundTag standbyGear = new CompoundTag();
        standbyGear.put("legs", player.getItemBySlot(EquipmentSlot.LEGS).save(new CompoundTag()));
        standbyGear.put("mainhand", player.getItemBySlot(EquipmentSlot.MAINHAND).save(new CompoundTag()));
        PlayerDataManager.get(player).setStandbyGear(standbyGear);

        // 装备完整装甲套装
        config.equipFullArmorSet(player);

        // 移除主手物品
        HenshinUtils.removeMainhandItem(player,config.getMainHenshinItem());

        // 播放变身音效
        config.playSound(player, SoundType.TRANSFORM);

        // 设置状态和消息
        config.setCurrentState(player, HenshinState.ACTIVE);
        HenshinUtils.sendMessage(player, "§a" + config.getRiderName() + " 快速变身完成!", ChatFormatting.GREEN);

        // 保存数据
        PlayerDataManager.get(player).saveToPlayer(player);
    }

    @Override
    public void handleDeactivation(Player player, BaseRiderConfig config) {
        HenshinState currentState = config.getCurrentState(player);

        // 停止音效
        SoundEvent driverSound = config.getSoundMap().get(SoundType.DRIVER);
        if (driverSound != null) {
            HenshinUtils.stopSound(player, driverSound);
        }

        if (currentState == HenshinState.STANDBY) {
            restoreStandbyGear(player, config);
        } else if (currentState == HenshinState.ACTIVE) {
            restoreActiveGear(player, config);
        }

        PlayerDataManager.get(player).clearGearData();
        config.setCurrentState(player, HenshinState.INACTIVE);
        config.playSound(player, SoundType.DEACTIVATION);
        HenshinUtils.sendMessage(player, "§a" + config.getRiderName() + " 变身解除!", ChatFormatting.YELLOW);

        // 保存数据
        PlayerDataManager.get(player).saveToPlayer(player);
    }

    @Override
    public void enterStandbyState(Player player, BaseRiderConfig config) {
        CompoundTag standbyGear = new CompoundTag();
        standbyGear.put("legs", player.getItemBySlot(EquipmentSlot.LEGS).save(new CompoundTag()));
        standbyGear.put("mainhand", player.getItemBySlot(EquipmentSlot.MAINHAND).save(new CompoundTag()));
        PlayerDataManager.get(player).setStandbyGear(standbyGear);

        config.equipBelt(player);
        HenshinUtils.removeMainhandItem(player,config.getMainHenshinItem());
        config.playSound(player, SoundType.STANDBY);
        config.setCurrentState(player, HenshinState.STANDBY);
        HenshinUtils.sendMessage(player, "§a待机状态: " + config.getRiderName(), ChatFormatting.YELLOW);
    }

    private void completeTransformation(Player player, BaseRiderConfig config) {
        // 停止待机音效
        SoundEvent standbySound = config.getSoundMap().get(SoundType.STANDBY);
        if (standbySound != null) {
            HenshinUtils.stopSound(player, standbySound);
        }

        CompoundTag activeGear = new CompoundTag();
        activeGear.put("head", player.getItemBySlot(EquipmentSlot.HEAD).save(new CompoundTag()));
        activeGear.put("chest", player.getItemBySlot(EquipmentSlot.CHEST).save(new CompoundTag()));
        activeGear.put("feet", player.getItemBySlot(EquipmentSlot.FEET).save(new CompoundTag()));
        PlayerDataManager.get(player).setActiveGear(activeGear);

        config.equipFullArmorSet(player);
        config.setCurrentState(player, HenshinState.ACTIVE);
        config.playSound(player, SoundType.TRANSFORM);
        HenshinUtils.sendMessage(player, "§a" + config.getRiderName() + " 变身完成!", ChatFormatting.GREEN);
    }

    private void restoreStandbyGear(Player player, BaseRiderConfig config) {
        CompoundTag standbyGear = PlayerDataManager.get(player).getStandbyGear();
        if (standbyGear != null) {
            if (standbyGear.contains("legs")) {
                player.setItemSlot(EquipmentSlot.LEGS,
                        ItemStack.of(standbyGear.getCompound("legs")));
            }
            if (standbyGear.contains("mainhand")) {
                player.setItemSlot(EquipmentSlot.MAINHAND,
                        ItemStack.of(standbyGear.getCompound("mainhand")));
            }
        }
    }

    private void restoreActiveGear(Player player, BaseRiderConfig config) {
        CompoundTag activeGear = PlayerDataManager.get(player).getActiveGear();
        if (activeGear != null) {
            if (activeGear.contains("head")) {
                player.setItemSlot(EquipmentSlot.HEAD,
                        ItemStack.of(activeGear.getCompound("head")));
            }
            if (activeGear.contains("chest")) {
                player.setItemSlot(EquipmentSlot.CHEST,
                        ItemStack.of(activeGear.getCompound("chest")));
            }
            if (activeGear.contains("feet")) {
                player.setItemSlot(EquipmentSlot.FEET,
                        ItemStack.of(activeGear.getCompound("feet")));
            }
        }

        restoreStandbyGear(player, config);
    }
}